#import sys
#from PyQt4.QtCore import Qt
#from PyQt4.QtGui import *
#
#app = QApplication([])
#tableWidget = QTableWidget()
#tableWidget.setContextMenuPolicy(Qt.ActionsContextMenu)
#
#quitAction = QAction("Quit", None)
#quitAction.triggered.connect(qApp.quit)
#tableWidget.addAction(quitAction)
#
#tableWidget.show()
#sys.exit(app.exec_())




#import sys
#from PyQt4.QtCore import Qt
#from PyQt4.QtGui import *
#
#class TableWidget(QTableWidget):
#
#    def __init__(self, parent = None):
#    
#        QTableWidget.__init__(self, parent)
#        self.setContextMenuPolicy(Qt.ActionsContextMenu)
#        
#        quitAction = QAction("Quit", self)
#        quitAction.triggered.connect(qApp.quit)
#        self.addAction(quitAction)
#
#app = QApplication([])
#tableWidget = TableWidget()
#tableWidget.show()
#sys.exit(app.exec_())

#
#import sys
#from PyQt4.QtCore import Qt
#from PyQt4.QtGui import *
#
#class TableWidget(QTableWidget):
#
#    def __init__(self, parent = None):
#    
#        QTableWidget.__init__(self, parent)
#    
#    def contextMenuEvent(self, event):    
#        menu = QMenu(self)
#        quitAction = menu.addAction("Quit222")
#        action = menu.exec_(self.mapToGlobal(event.pos()))
#        if action == quitAction:
#            qApp.quit()
#
#app = QApplication([])
#tableWidget = TableWidget()
#tableWidget.show()
#sys.exit(app.exec_())


import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

data = [('Alice', [("Keys", []),("Purse", [("Cellphone", [])])]),
    ("Bob", [("Wallet", [("Credit card", []),("Money", [])])])]

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.treeView = QTreeView()
        self.treeView.setContextMenuPolicy(Qt.CustomContextMenu)
        self.treeView.customContextMenuRequested.connect(self.openMenu)

        self.model = QStandardItemModel()
        self.addItems(self.model, data)
        self.treeView.setModel(self.model)

        self.model.setHorizontalHeaderLabels([self.tr("Object")])

        layout = QVBoxLayout()
        layout.addWidget(self.treeView)
        self.setLayout(layout)

    def addItems(self, parent, elements):
        for text, children in elements:
            item = QStandardItem(text)
            parent.appendRow(item)
            if children:
                self.addItems(item, children)

    def openMenu(self, position):
        print position
        indexes = self.treeView.selectedIndexes()
        print indexes
        if len(indexes) > 0:
            level = 0
            index = indexes[0]
            while index.parent().isValid():
                index = index.parent()
                level += 1
                
        menu = QMenu()
        if level == 0:
            menu.addAction(self.tr("Edit person"))
        elif level == 1:
            menu.addAction(self.tr("Edit object/container"))
        elif level == 2:
            menu.addAction(self.tr("Edit object"))
         
        menu.exec_(self.treeView.viewport().mapToGlobal(position))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

        


